package com.gqz.aircraftwar.util

import android.graphics.*
import kotlin.math.abs
import kotlin.math.pow
import kotlin.math.sqrt

/**
 * 计算工具类
 *
 * @author gqz
 * @data 2020/3/24
 */
object CalcUtils {
    /**
     * 计算移动速度，任意方向
     */
    internal fun calcSpeed(start: PointF, end: PointF): FloatArray {
        var speedx = 0f
        var speedy = 0f

        var x = abs(start.x - end.x)
        var y = abs(start.y - end.y)

        if (x > y) {
            speedx = 1f
            speedy = 1f * (y / x)
        } else if (x < y) {
            speedx = 1f * (x / y)
            speedy = 1f
        } else {
            speedx = 1f
            speedy = 1f
        }
        if (end.x < start.x) {
            speedx = -speedx
        }
        if (end.y < start.y) {
            speedy = -speedy
        }
        return floatArrayOf(speedx, speedy)
    }

    /**
     * 计算两点距离
     */
    internal fun distance(p1: PointF, p2: PointF): Float {
        var x = abs(p1.x - p2.x)
        var y = abs(p1.y - p2.y)
        var distance = sqrt(x.pow(2) + y.pow(2))
        return distance
    }

    /**
     * 计算直线上指定距离的坐标
     */
    internal fun getPosOnLine(start: PointF, end: PointF, distance: Float): PointF {
        var path = Path()
        path.moveTo(start.x, start.y)
        path.lineTo(end.x, end.y)
        var measure = PathMeasure(path, false)
        var posArr = FloatArray(2)
        measure.getPosTan(distance, posArr, null)
        return PointF(posArr[0], posArr[1])
    }

    /**
     * 碰撞检测
     */
    internal fun intersect(rect: Rect, bmp: Bitmap) {

    }

}


